package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.RcglInvestigateConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglInvestigateCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglInvestigateQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglInvestigateResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglInvestigateUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglInvestigateEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglInvestigateMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglInvestigateService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * 实地查访
 *
 * @author wenjie
 * @date 2024-05-30 12:02:59
 */
@Service
public class RcglInvestigateServiceImpl extends ServiceImpl<RcglInvestigateMapper, RcglInvestigateEntity> implements RcglInvestigateService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<RcglInvestigateResultDTO> getPageList(RcglInvestigateQueryDTO dto) {
        Page<RcglInvestigateEntity> page = page(Pages.page(dto), wrapper(dto));
        return Pages.convert(page, RcglInvestigateConvert.INSTANCE::convert);
    }

    @Override
    public RcglInvestigateResultDTO getById(Long id) {
        RcglInvestigateResultDTO result = RcglInvestigateConvert.INSTANCE.convert(super.getById(id));
        //设置法律文书信息
        this.setInstrumentList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(RcglInvestigateCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        RcglInvestigateEntity entity = RcglInvestigateConvert.INSTANCE.create(dto, dx.getTenantId());
        super.save(entity);
        //设置法律文书信息
        this.saveInstrumentList(dto, entity, user);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(RcglInvestigateUpdateDTO dto) {
        RcglInvestigateEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        RcglInvestigateEntity entity = RcglInvestigateConvert.INSTANCE.update(dto);
        super.updateById(entity);
        //保存文件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    public void saveInstrumentList(RcglInvestigateCreateDTO dto, RcglInvestigateEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_INVESTIGATE), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(RcglInvestigateUpdateDTO dto, RcglInvestigateEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_INVESTIGATE), dto.getInstrumentList(), user);
    }

    private void setInstrumentList(RcglInvestigateResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_INVESTIGATE);
        dto.setInstrumentList(fileService.findList(query));
    }

    private LambdaQueryWrapper<RcglInvestigateEntity> wrapper(RcglInvestigateQueryDTO query) {
        LambdaQueryWrapper<RcglInvestigateEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(RcglInvestigateEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(RcglInvestigateEntity::getRybh, v));
        if (Objects.nonNull(query.getStartTime()) && Objects.nonNull(query.getEndTime())) {
            wrapper.between(RcglInvestigateEntity::getCfsj, query.getStartTime(), query.getEndTime());
        }
        return wrapper;
    }
}